<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

/**
 * 直通车报表数据API
 */

class RptCampaignModel extends RptBaseModel
{

	protected $tableName = 'rpt_campaign';
	/** @var object */
	private $me = null;
	/** @var object */
	private $subwayToken = null;

	/**
	 * 初始化。主要检查session['me']。
	 *
	 */
	public function _initialize()
	{
		parent :: _initialize();
		$this->me = session('me');
	}

	/**
	 * 获取给定用户给定推广计划给定日期的花费.
	 * 
	 * @param int $user_id 用户ID.
	 * @param int $campaign_id [可选] 如果不给定, 查询用户所有推广计划. 默认为全部推广计划.
	 * @param string $date [可选] 日期, 格式为date('Y-m-d'). 默认为昨天. 
	 * @return float 花费(元)
	 */
	public function getCost($user_id, $campaign_id = 0, $date = 0)
	{
		if(!$date)
			$date = date('Y-m-d', time() - 86400);
		$whr = array(
				'taobao_user_id' => $user_id,
				'date' => $date,
		);
		if($campaign_id > 0) {
			$whr['campaign_id'] = $campaign_id;
		}
		$res = $this->where($whr)->getField('sum(`cost`)');
		if($res === false) {
			throw_exception('数据库查询出错.');
		} else if($res) {
			return round($res/100, 2);
		} else {
			return 0;
		}
	}


	/**
	 *  get the statics information of a campaign for current user
	 *  
	 * @param int campaign id, 0 means total  
	 * @param string or array, the indicators needed: 
	 * 						  impressions 展现量,roi,投入产出比 = sales / cost , cost 花费 ,
	 * 						  direct_pay , 直接成交 , indirect_pay 间接成交
	 * 						  direct_pay_count , 直接成交数, indirect_pay_count 间接成交数
	 * 						  roc 转化率 =产生购买行为的客户人数 / 所有到达店铺的访客人数
	 * 						  sales 销售额=direct_pay+indirect_pay, click  点击量 , ctr 点击率 , cpm 千次展现费 ;
	 * 						avgpos 平均展现排名
	 * @param string , date string of the start date , format Y-m-d H:i:s , 
	 * @param string , date strign of the end date , format  Y-m-d H:i:s
	 * @param string , type of information needed, 
	 * 					opts:
	 *					"summary" 只要总计结果 , 
	 * 					"series"  分时间的序列结果
	 *					"summary,series" 两个结果都要 		
	 * 
	 * @return mixed of array, 
	 * 					example:
	 *  
	 * 
	 */
	public function getAllCampaignsStat($campaign_id, $indicators = '', $from = '', $to = '', $type='summary,series')
	{
	    //check sync status
	    // $this->syncCampaignReport( $this->me['user_id'] , $campaign_id);
		return $this->getStats($this->me['user_id'], $campaign_id, 
				$indicators, $from, $to, $type);
	}

	/**
	 * 同步给定推广计划的报表数据
	 * 
	 * @param int $user_id ,必须， 用户的id
	 * @param int $campaign_id 某个推广计划id， 一次只能一个 
	 * @return void , 没有更新时返回false
	 */
	public function syncCampaignReport( $user_id  , $campaign_id)
	{
		$entity = 'rpt-campaign:' . $campaign_id;
		$time_slot = SyncModel::getSyncTimeSlot($user_id, $entity);
		if($time_slot){
			$rptMdl = new RptModel();
			$rptMdl->syncSummaryData($user_id, 'rpt_campaign', 
					$campaign_id, 0, $time_slot['start'] , $time_slot['end']);
		}else{
			return false;
		}
		$condition = array(
					'taobao_user_id' => $user_id,
					'campaign_id' => $campaign_id
				);
		$updated= $this->where($condition)->order('date DESC')->getField('date');
		if($updated) {
		    //记录得到的数据的最后一天时间作为更新时间
		    SyncModel::setLatestUpdated($user_id , $entity ,$updated);
		}
		return true;
	}
}
